package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.CalculateMarginBean;
import com.sikao.app.bean.CalculateMarginSubmitBean;
import com.sikao.app.bean.InviteDetailBean;
import com.sikao.app.bean.SellerGoBidBean;
import com.sikao.app.bean.SellerGoBidSubmitBean;
import com.sikao.app.dao.CalculateMarginModel;
import com.sikao.app.dao.SellerGoBidModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;

import java.text.DecimalFormat;

/**
 * 招标下单
 */
public class TenderOrderActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private Button orderBtn;
	private TextView tvNumberUnit;
	private EditText etNumber;
	private EditText etPrice;
	private EditText etRemark;
	private TextView tvAmount;
	private TextView tvMargin;
	private String number = "";
	private String price = "";

	private CalculateMarginSubmitBean calculateMarginSubmitBean = new CalculateMarginSubmitBean();
	private String inviteId = "";
	private String leastNumber = "";
	private String needNumber = "";
	private String unit = "";
	private String role = "seller";
	//计算的type type: goods | invite | auction  保证金挂单 商城 招标 拍卖
	private String type = "invite";
	double money = 0;
	private String cash = "";
	private String payPassword = "";
	public static final int PAY_REQUEST_CODE = 10002;//保证金付款请求的code
	public static final int PAY_RESULT_CODE = 100020;//保证金付款正常返回的code
	private DecimalFormat format = new DecimalFormat("#.###");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_tender_order);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		Intent intent = getIntent();
		inviteId = intent.getStringExtra("inviteId");
		leastNumber = format.format(Double.valueOf(intent.getStringExtra("leastNumber")));
		needNumber = format.format(Double.valueOf(intent.getStringExtra("number")));
		unit = intent.getStringExtra("unit");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		orderBtn = (Button) findViewById(R.id.order_btn);
		tvNumberUnit = (TextView) findViewById(R.id.tv_number_unit);
		etNumber = (EditText) findViewById(R.id.et_number);
		etPrice = (EditText) findViewById(R.id.et_price);
		etRemark = (EditText) findViewById(R.id.et_remark);
		tvAmount = (TextView) findViewById(R.id.tv_amount);
		tvMargin = (TextView) findViewById(R.id.tv_margin);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		orderBtn.setOnClickListener(this);

		etNumber.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {

				calculateMargin();

			}
		});
		etPrice.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {

				calculateMargin();

			}
		});
	}

	@Override
	protected void init() {
		titleTv.setText("立即下单");
		backIv.setVisibility(View.VISIBLE);
		tvNumberUnit.setText("竞标数量(" + unit + ")：");
		etNumber.setHint("需求量" + needNumber + unit + ",至少供应" + leastNumber + unit);
		// calculateMargin();
	}

	private void calculateMargin() {//计算保证金 {"role":"seller","type":"invite","money":"10000","objId":""}

		number = etNumber.getText().toString();
		price = etPrice.getText().toString();
		if (!Utils.isNumber(number)) {
			return;
		}
		if (!Utils.isNumber(price)) {
			return;
		}

		money = Double.parseDouble(number) * Double.parseDouble(price);

		tvAmount.setText(money + "");

		if (money > 0) {
			// 保证金 "role":"","type":"","money":"10000","objId":""}

			calculateMarginSubmitBean.setRole(role);
			calculateMarginSubmitBean.setType(type);
			calculateMarginSubmitBean.setMoney(Utils.roundForNumber(money + "", 3));
			calculateMarginSubmitBean.setObjId(inviteId);

			new CalculateMarginModel(this, new OnNetBackListener<CalculateMarginBean>() {
				@Override
				public void netBack(CalculateMarginBean data) {
					if (data == null) {
						Toast.makeText(TenderOrderActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					} else {
						if (data.getStatus() == 1) {
							// tvCash.setText(data.getRes().getCash());
							tvMargin.setText(data.getRes().getMargin());
							cash = data.getRes().getCash();
						} else {
							Toast.makeText(TenderOrderActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				}
			}).request(JSONUtil.toJSON(calculateMarginSubmitBean), false);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			case R.id.order_btn:
				payCheck();
				break;
			default:
				break;
		}
	}

	private void payCheck() {
		if (TextUtils.isEmpty(etNumber.getText())) {
			Utils.showToast("数量不能为空");
			return;
		}
		if (TextUtils.isEmpty(etPrice.getText())) {
			Utils.showToast("报价不能为空");
			return;
		}
		if (TextUtils.isEmpty(etRemark.getText())) {
			Utils.showToast("备注不能为空");
			return;
		}
		if (etRemark.getText().toString().length() < 10) {
			Utils.showToast("备注需10~500字");
			return;
		}
		if (TextUtils.isEmpty(tvMargin.getText())) {
			Utils.showToast("数据异常请返回重试");
			return;
		}

		Intent intentPay = new Intent();
		intentPay.setClass(this, PayForReleaseGoodsActivity.class);

		intentPay.putExtra("publishType", 2);
		//保证金的
		intentPay.putExtra("geld", Utils.roundForNumber(tvMargin.getText().toString(), 2));
		intentPay.putExtra("balance", Utils.roundForNumber(cash, 2));

		startActivityForResult(intentPay, PAY_REQUEST_CODE);
	}

	private void sellerGoBid() {//卖家投标 {"inviteId":"81","number":"12.000","price":"68.00","amount":"816","remark":"这是我的最低报价了","payPassword":"123456","margin":"123.00"}


		SellerGoBidSubmitBean sellerGoBidSubmitBean = new SellerGoBidSubmitBean();
		sellerGoBidSubmitBean.setInviteId(inviteId);
		sellerGoBidSubmitBean.setNumber(Utils.roundForNumber(number, 3));
		sellerGoBidSubmitBean.setPrice(Utils.roundForNumber(price, 2));
		sellerGoBidSubmitBean.setAmount(Utils.roundForNumber(money + "", 2));
		sellerGoBidSubmitBean.setRemark(etRemark.getText().toString());
		sellerGoBidSubmitBean.setMargin(Utils.roundForNumber(tvMargin.getText().toString() + "", 2));
		sellerGoBidSubmitBean.setPayPassword(payPassword);

		new SellerGoBidModel(this, new OnNetBackListener<SellerGoBidBean>() {
			@Override
			public void netBack(SellerGoBidBean data) {
				if (data == null) {
					Toast.makeText(TenderOrderActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						Toast.makeText(TenderOrderActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						TenderDetailsActivity.tenderDetailsActivity.finish();
						finish();
					} else {
						Toast.makeText(TenderOrderActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}

			}
		}).request(JSONUtil.toJSON(sellerGoBidSubmitBean), true);


	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
			case PAY_REQUEST_CODE://是付款返回的
				if (resultCode == PAY_RESULT_CODE) {//正常付款返回的
					payPassword = data.getStringExtra("payPassword");
					sellerGoBid();
				}
				break;
		}
	}
}
